﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using SATF.BI;
using System.Data;
using SATF.DB;

namespace SATF
{
    public partial class Logon : System.Web.UI.Page
    {
        private LogonInfosSingleton m_logonInfosSingleton;

        protected void Page_Load(object sender, EventArgs e)
        {       
            if (!IsPostBack)
            {
                if (Request.Cookies["satf_userinfo"] != null)
                {
                    if (Request.Cookies["satf_userinfo"]["user"] != null)
                    {
                        this.tbxUserName.Text = Request.Cookies["satf_userinfo"]["user"];
                    }

                    if (Request.Cookies["satf_userinfo"]["pwd"] != null)
                    {
                        this.tbxPassword.Text = EncryptDecrypt.DecryptDES(Request.Cookies["satf_userinfo"]["pwd"]);
                    }
                    
                    if (Request.Cookies["satf_userinfo"]["auto"] != null)
                    {
                        this.cbxAutoLogon.Checked = Convert.ToBoolean(Request.Cookies["satf_userinfo"]["auto"]);
                    }
                    
                    if (this.cbxAutoLogon.Checked)
                    {
                        btnLogin_Click(null, null);
                        return;
                    }

                }
            }

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (CheckUserAndPwd())
            {
                SaveCookie(this.cbxAutoLogon.Checked);
                Response.Redirect("../Default.aspx");
            }
            else
            {
                
            }
        }

        private bool CheckUserAndPwd()
        {
            m_logonInfosSingleton = LogonInfosSingleton.instance;
            string userName = this.tbxUserName.Text;
            string pwd = this.tbxPassword.Text;
            if (userName.IndexOf("'") > -1 || userName.IndexOf(' ') > -1 || userName.IndexOf('"') > -1
                || pwd.IndexOf("'") > -1 || pwd.IndexOf(' ') > -1 || pwd.IndexOf('"') > -1)
            {
                Alert.ShowInTop("用户名或密码包含有非法字符！", MessageBoxIcon.Warning);
                return false;
            }
            string pwd_encrypt = EncryptDecrypt.EncryptDES(pwd);
            BIUser user = new BIUser(userName);
            if (user == null || user.ID < 1)
            {
                Alert.ShowInTop("无此用户！", MessageBoxIcon.Warning);
                return false;
            }

            if (user.Pwd == pwd_encrypt)
            {
                LogonInfo logonInfo = new LogonInfo();
                logonInfo.UserName = userName;
                logonInfo.PwdEncrypt = pwd_encrypt;
                logonInfo.IsLogon = true;
                logonInfo.BIUser = user;
                LogonInfosSingleton.AddItem(logonInfo);
                return true;
            }
            else
            {
                Alert.ShowInTop("用户名或密码错误！", MessageBoxIcon.Error);
                return false;
            }
        }

        private void SaveCookie(bool autoLogon)
        {
            HttpCookieCollection cookies = Request.Cookies;
            HttpCookie cookie = new HttpCookie("satf_userinfo");
            cookie.Values["user"] = this.tbxUserName.Text;
            cookie.Values["pwd"] = EncryptDecrypt.EncryptDES(this.tbxPassword.Text);
            cookie.Values["auto"] = this.cbxAutoLogon.Checked.ToString();
            cookie.Values["last"] = System.DateTime.Now.ToString();
            if (autoLogon)
            {
                cookie.Expires = DateTime.MaxValue; 
            }
            else
            {
                //设置过期时间1天
                cookie.Expires = System.DateTime.Now.AddDays(1);
            }
            Response.Cookies.Add(cookie);

            //设置session
            Page.Session.RemoveAll();
            Page.Session.Add("user", this.tbxUserName.Text);

        }


    }
}